const GitServer = require('./GitServer');
const GiteeRequest = require('./GiteeRequest');

class Gitee extends GitServer {
    constructor() {
        super('gitee');
    }

    // 返回生成token的连接，在外面提示 a3c5eb74382fd29f4311b4059806d6a4
    getTokenHelpUrl = () => {
        return 'https://gitee.com/profile/personal_access_tokens';
    };

    // 设置token
    setToken = (token) => {
        this.request = new GiteeRequest(token);
    };

    // 获取用户
    getUser = () => {
        return this.request.get('/user').then(response => {
            return this.handleResponse(response);
        });
    };

    // 获取远程仓库
    getRepo = (owner, repo) => {
        return this.request.get(`/repos/${owner}/${repo}`).then(response => {
            return this.handleResponse(response);
        });
    };

    // 创建远程个人仓库
    createRepo = (repo) => {
        return this.request.post('/user/repos', {
            name: repo,
        });
    };

    // 创建远程组织仓库
    createOrgRepo = (repo, login) => {
        return this.request.post(`/orgs/${login}/repos`, {
            name: repo,
        });
    };
    
    // 获取远程仓库地址
    getRemote = (login, repo) => {
        return `git@gitee.com:${login}/${repo}.git`;
    };

    getSSHKeysUrl = () => {
        return 'https://gitee.com/profile/sshkeys';
    };

    getSSHKeysHelpUrl = () => {
        return 'https://gitee.com/help/articles/4191';
    };

    getOrgs = () => {
        return this.request.get('/user/orgs', {
            page: 1,
            per_page: 100,
            admin: true,
        }).then(response => {
            return this.handleResponse(response);
        });
    };
}

module.exports = Gitee;
